package one.stand.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import one.stand.mapper.BusinessCardMapperExt;
import one.stand.mapper.BusinessCardPublicMapper;
import one.stand.model.BusinessCard;
import one.stand.model.BusinessCardModel;
import one.stand.model.Company;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanyBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@Service
public class BusinessCardBaseServiceImpl implements BusinessCardBaseService {
    @Autowired
    private BusinessCardMapperExt businessCardMapperExt;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private BusinessCardPublicMapper businessCardPublicMapper;

    @Override
    public BusinessCard getCustomer(Integer cardId) {
        Integer id = cardId;
        if (Objects.nonNull(cardId) && cardId < 0) {//企业名片
            id = businessCardPublicMapper.selectCompanyCard(cardId * -1);
        }
        return Objects.nonNull(id) ? businessCardMapperExt.selectByPrimaryKey(id) : null;
    }

    @Override
    public BusinessCard getByUserId(Integer userId) {
        return businessCardPublicMapper.selectByUserId(userId);
    }

    @Override
    public List<BusinessCard> getByCompanyId(Integer companyId) {
        return businessCardMapperExt.selectByCompanyId(companyId);
    }

    @Override
    public Integer count(Integer companyId) {
        PageHelper.startPage(1, 1);
        List<BusinessCard> cards = getByCompanyId(companyId);
        return Math.toIntExact(((Page) cards).getTotal());
    }

    @Override
    public BusinessCard get(String personalWx, String phone, Integer companyId, Integer myCardId) {
        return businessCardPublicMapper.selectMyCard(new BusinessCardModel() {{
            setPersonalWx(personalWx);
            setPhone(phone);
            setCompanyId(companyId);
            setIsDel(0);
            setState(1);
            setMyCardId(myCardId);
        }});
    }

    @Override
    public BusinessCardModel getCustomer(String personalWx, String phone, Integer companyId, Integer myCardId) {
        return businessCardPublicMapper.selectRepeatInfo(new BusinessCardModel() {{
            setPersonalWx(personalWx);
            setPhone(phone);
            setCompanyId(companyId);
            setMyCardId(myCardId);
        }});
    }

    @Override
    public Integer countCustomer(Integer companyId) {
        return businessCardMapperExt.selectCountCustomer(companyId);
    }

    @Override
    public Double getRateRank(Integer cardId) {
        return businessCardPublicMapper.selectRateRank(cardId);
    }

    @Override
    public Integer getBoosCardId(Integer companyId) {
        Company company = companyBaseService.getById(companyId);
        BusinessCard card = getByUserId(company.getUserId());
        return card.getCardId();
    }
}
